﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;

namespace BarcodeHandlerForms
{
    public partial class ChangePasswordForm : Form
    {
        public ChangePasswordForm()
        {
            InitializeComponent();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            string oldPassword = this.OldPassword.Text;
            string validatePassword = this.txtConfirmPassword.Text;
            string password = this.txtPassword.Text;

            if (string.IsNullOrWhiteSpace(oldPassword))
            {
                MessageBox.Show("请输入原始密码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (string.IsNullOrWhiteSpace(password))
            {
                MessageBox.Show("请输入密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (string.IsNullOrWhiteSpace(validatePassword))
            {
                MessageBox.Show("请输入验证密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (password != validatePassword)
            {
                MessageBox.Show("两次密码输入不一致", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            oldPassword = Utils.GetSHA1Str(oldPassword);

            string configPassword = Utils.GetConfiguration().AppSettings.Settings["Password"].Value;

            if (configPassword != oldPassword)
            {
                MessageBox.Show("密码不正确", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Configuration config = Utils.GetConfiguration();
            if (config != null)
                config.AppSettings.Settings["Password"].Value = Utils.GetSHA1Str(password);
            config.Save(ConfigurationSaveMode.Modified);

            MessageBox.Show("密码修改成功！","提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
